/*
 * Copyright (c) $today.year-Now http://www.zxpnet.com All rights reserved.
 */

package com.zxp.springboot.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zxp.common.api.R;
import com.zxp.common.constant.module.CacheConstants;
import com.zxp.common.utils.SpringContextHolder;
import com.zxp.springboot.pojo.HelloUser;
import com.zxp.springboot.service.CalcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @desc:
 * @author: shollin
 * @Date: 2021/6/15/015 11:19
 */
@RestController
@Slf4j
public class HelloController {

    @GetMapping("/hello")
    public String hello1() {
        return "Springboot2 jrebel启动";
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CalcService calcService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @GetMapping("/file")
    public FileSystemResource resource() {
        File file = new File("E:\\svn\\gmzx\\document\\note笔记\\vuepress-blog\\docs\\index.md");
        FileSystemResource fileSystemResource = new FileSystemResource(file);

        return fileSystemResource;
    }

    @GetMapping(name = "xml",produces = "application/xml; charset=UTF-8")
    public Object json() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("a", "aa");
        return jsonObject;
    }

    /**
     * 返回map, 测试序列化
     *
     * @return
     */
    @GetMapping("/view")
    public R view() {
        HashMap<Object, Object> map = Maps.newHashMap();
        map.put("aa", "aa");
        map.put("long", 12345649687987354L);
        map.put("uuid", IdUtil.randomUUID());
        map.put("cache", CacheConstants.CLIENT_DETAILS_KEY);

        HelloUser helloUser = new HelloUser();
        helloUser.setId(1221324313544350L);
        helloUser.setUsername("jim");
        helloUser.setAge(123);
        helloUser.setSalary(new BigDecimal("1523.6501"));
        helloUser.setCreateTime(RandomUtil.randomDay(-365 * 5, 365 * 5));
        helloUser.setBirthday(LocalDate.now());
        helloUser.setUpdateTime(LocalDateTime.now());
        helloUser.setParent(null);
        map.put("helloUser", helloUser);
        ArrayList<HelloUser> users = Lists.newArrayList();

        users.add(helloUser);

        for (int i = 0; i < 10; i++) {
            HelloUser u = new HelloUser();
            u.setId(RandomUtil.randomLong(12));
            u.setUsername(RandomUtil.randomNumbers(6));
            u.setAge(RandomUtil.randomInt(10, 60));
            u.setSalary(new BigDecimal(RandomUtil.randomDouble(3000, 50000, 2, RoundingMode.HALF_UP)));
            u.setCreateTime(RandomUtil.randomDay(-365 * 5, 365 * 5));
            u.setBirthday(LocalDate.now());
            u.setUpdateTime(LocalDateTime.now());
            u.setParent(users.get(i));

            users.add(u);
        }

        map.put("listUser", users);
        return R.data(map);
    }

    @GetMapping("/rest")
    public String testRestTemplate() {
        RestTemplate bean = SpringContextHolder.getBean(RestTemplate.class);
        log.info(restTemplate.toString());
        return bean.toString();
    }

    // 测试分布式锁
    @GetMapping("/lock")
    public String lock() {
        String uuid = UUID.randomUUID().toString();
        Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", uuid, 3, TimeUnit.SECONDS);

        if (lock) {
            log.debug("拿到锁了..." + Thread.currentThread().getId());
            Integer n = (Integer) redisTemplate.opsForValue().get("num");
            if (n == null) {
                redisTemplate.opsForValue().set("num", 1);
            } else {
                redisTemplate.opsForValue().increment("num");
            }
            // 删除锁，这里需要是原子性操作，改成执行lua脚本
            if (uuid.equals(redisTemplate.opsForValue().get("lock"))) {
                redisTemplate.delete("lock");
            }
        }
        return "lock";
    }


    @GetMapping("/aop")
    public void testAop() {
        calcService.div(10, 2);

        log.info("执行成功");
    }

}
